import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from tools import common_assert, html_parser, read_json


class TestTender:
    # 初始化
    def setup_method(self):
        # 获取session对象
        log.info("获取session对象")
        self.session = requests.session()
        # 获取ApiTender对象
        log.info("获取ApiTender对象")
        self.api = ApiTender(session=self.session)
        # 获取ApiRegisterLogin对象
        log.info("获取ApiRegisterLogin对象")
        self.login = ApiRegisterLogin(self.session)

    # 结束
    def teardown_method(self):
        # 关闭session对象
        log.info("关闭session对象")
        self.session.close()

    # 投资接口 测试
    @pytest.mark.parametrize("id,amount,expect_json,expect_text",read_json("tender.json","tender"))
    def test01_tender(self, id, amount, expect_json, expect_text):
        try:
            # 1、登录
            log.info("登录")
            self.login.api_login()
            # 2、请求后台投资
            result = self.api.url_tender(id=id, amount=amount)
            log.info("后台投资返回结果为：{}".format(result.json()))
            # 后台投资 断言
            common_assert(result, expect_json=expect_json)
            log.info("后台投资断言通过")
            # 3、三方投资
            if "金额" in expect_json :
                return
            else:
                data = html_parser(result)
                result2 = self.session.post(url=data[0], data=data[1])
                log.info("三方投资返回结果为：{}".format(result2.text))
                # 三方投资 断言
                common_assert(result2, expect_text=expect_text)
                log.info("三方投资断言通过")
        except Exception as e:
            log.error(e)
            raise
